/*
* 使用权资产页签
* */
import ampub from 'ampub';
import moment from 'moment';
import { enableMonthlyInterest } from '.';
import constants from '../../constants';


const { utils: { multiLangUtils: { getMultiLangByID }, msgUtils: { showMessage } } } = ampub;
const {
	UISTATE,
	SAMEHEADS,
	AREA: { CARD: { BASIC_INFO_AREA: basicInfoForm, USUFRUCTASSET_AREA: usufructasset_tab } }
} = constants;

/**
 * 开启使用权页签
 */
function enableUsufructAssetTab() {
	const { props: { meta, form, getUrlParam } } = this;
	const status = getUrlParam('status');
    const { ADD, EDIT, BROWSE, BLANK } = UISTATE;
    // 恢复按月计息页签，新增态
    if(ADD == status){
        enableMonthlyInterest.call(this);
    }
    
    handleMeta.call(this, true);

	form.setAreaVisible(basicInfoForm, usufructasset_tab, true); // 只有2012有封装该方法
	/* //TODO 2005和1909代替方法
    let meta0 = meta.getMeta();
    meta0[usufructasset_tab].areaVisible = true;
    meta0[usufructasset_tab].isunfold = true;
    meta.setMeta(meta0);
    */
}

/**
 * 关闭使用权页签
 */
function disableUsufructAssetTab() {
	const { props: { meta, form } } = this;

	form.setAreaVisible(basicInfoForm, usufructasset_tab, false); // 只有2012有封装该方法
	/* //TODO 2005和1909代替方法
    let meta0 = meta.getMeta();
    meta0[usufructasset_tab].areaVisible = false;
    meta0[usufructasset_tab].isunfold = false;
    meta.setMeta(meta0);
    */
	// 清空使用权资产卡片
	emptyUsufructAssetTab.call(this);
}

/**
 * 清空使用权form
 */
function emptyUsufructAssetTab() {
	const { usufructasset_tab_fields, props: { meta, form } } = this;
	let zero = {};
	for (let item of usufructasset_tab_fields) {
		zero[item] = { value: null };
	}
	form.setFormItemsValue(basicInfoForm, zero);
}

/**
 * 获取使用权from rows
 */
function getUsufructAssetTab() {
	const { usufructasset_tab_fields, props: { meta, form } } = this;
	let usufructasset_tab_fields_patch = new Set(usufructasset_tab_fields);
	//补充三个字段
	['pk_group','pk_org','pk_org_v'].forEach( field => usufructasset_tab_fields_patch.add(field));
	const rows = form.getAllFormValue(basicInfoForm).rows;
	const [ { values } ] = rows;
	const keys = Object.keys(values);

	for (let key of keys) {
		// 既不在使用权页签，也不在公共pk中的字段删除
		if (!usufructasset_tab_fields_patch.has(key) && !SAMEHEADS.includes(key)) {
			delete values[key];
		}
	}
    // console.log(rows[0].values.renewal_flag);
	return rows;
}

/**
 * 更新使用权页签数据
 * 
 * @param {使用权页签} rows 
 */
function setUsufructAssetTab(rows) {
	const { props: { form } } = this;
	//返回的数据中没有账簿数据，只更新基本区域form数据
	form.setAllFormValue({ [basicInfoForm]: { rows: rows } });
}

/**
 * 固定资产计租方式只支持月，但是通过资产拉单时计租方式支持多种，所以需要根据是否是使用权资产来动态修改meta
 */
function handleMeta(usufructasset_flag = false) {
	return new Promise((reslove) => {
		let meta = this.props.meta.getMeta();
		meta[usufructasset_tab].items.map((item) => {
			if (item.attrcode == 'rent_way') {
				//使用权资产并且不是资产租入
				if (usufructasset_flag && !this.bill_source_4A3A) {
					item.label = getMultiLangByID('201201504A-000082') /*计租方式（月）*/;
					item.datatype = '4';
					item.itemtype = 'number';
					item.min = 1;
				} else {
					item.label = getMultiLangByID('201201504A-000081') /*计租方式*/;
					item.datatype = '1';
					item.itemtype = 'input';
					delete item.min;
				}
			}
		});
		this.props.meta.setMeta(meta);
	});
}

/**
 * 简单追溯控制
 */
function simpleTraceabilityDominate(field = "nothing") {
	const { props, props: { form } } = this;
    const { ADD, EDIT, BROWSE, BLANK } = UISTATE;
    const status = props.getUrlParam('status');

	/* 控制简单追溯调整-衔接处理方法&追溯日期 */
    /* 续租选择权 */
	let renewal_flag = form.getFormItemsValue(basicInfoForm, 'renewal_flag').value || false;
	/* 起租日期 */
	let rentstart_date = form.getFormItemsValue(basicInfoForm, 'rentstart_date').value;
	let first_start = moment(rentstart_date, 'YYYY-MM-DD HH:mm:ss');
    /* 止租日期 */
    let ending_date = form.getFormItemsValue(basicInfoForm, 'ending_date').value;
    /* 续租截止日期 */
	let relet_ending_date = form.getFormItemsValue(basicInfoForm, 'relet_ending_date').value;
    if (renewal_flag) {
        ending_date = relet_ending_date;
    }
	let ending_date_moment = moment(ending_date, 'YYYY-MM-DD HH:mm:ss');
	/* 追溯日期 */
	let retrospective_date = form.getFormItemsValue(basicInfoForm, 'retrospective_date').value;
	let retrospective_date_moment = moment(retrospective_date, 'YYYY-MM-DD HH:mm:ss');
	if (first_start.isBefore(retrospective_date_moment) && retrospective_date_moment.isSameOrBefore(ending_date_moment)) {
		/* 启用衔接处理方法 */
		form.setFormItemsDisabled(basicInfoForm, { cohesion_process: false });
		/* 启用追溯日期 */
		form.setFormItemsDisabled(basicInfoForm, { retrospective_date: false });
	} else {
		/* 禁用衔接处理方法 */
		form.setFormItemsDisabled(basicInfoForm, { cohesion_process: true });
		/* 禁用追溯日期 */
		form.setFormItemsDisabled(basicInfoForm, { retrospective_date: true });
	}

    if ([EDIT, ADD].includes(status) && field != "cohesion_process") {
        // 止租日期小于追溯日期，自动改成追溯调整
        if (ending_date_moment.isBefore(retrospective_date_moment)){
            form.setFormItemsValue(basicInfoForm, { cohesion_process: {value : '2'} });
        }
    }

	/* 控制简单追溯调整-租金拆分 */
	/*衔接处理方法*/
	let cohesion_process = form.getFormItemsValue(basicInfoForm, 'cohesion_process').value;
	if (first_start.isBefore(retrospective_date_moment) && cohesion_process == '1') {
		/* 启用租金拆分 */
		form.setFormItemsDisabled(basicInfoForm, { rent_split_flag: false });
	} else {
		/* 禁用租金拆分 */
		form.setFormItemsDisabled(basicInfoForm, { rent_split_flag: true });
        /* 租金拆分取否 */
        form.setFormItemsValue(basicInfoForm, { rent_split_flag: { value: false, display: '' } });
	}

	/* 控制简单追溯调整-拆分金额（含税） */
	/*租金拆分*/
	let rent_split_flag = form.getFormItemsValue(basicInfoForm, 'rent_split_flag').value;
	if (first_start.isBefore(retrospective_date_moment) && cohesion_process == '1' && rent_split_flag) {
		/* 启用拆分金额（含税） */
		form.setFormItemsDisabled(basicInfoForm, { split_renttax: false });
		/* 必输拆分金额（含税） */
		form.setFormItemsRequired(basicInfoForm, { split_renttax: true });
	} else {
		/* 禁用拆分金额（含税） */
		form.setFormItemsDisabled(basicInfoForm, { split_renttax: true });
		/* 非必输拆分金额（含税） */
		form.setFormItemsRequired(basicInfoForm, { split_renttax: false });
        /* 置空拆分金额（含税） */
        form.setFormItemsValue(basicInfoForm, { split_renttax: { value: '', display: '' } });
	}
}

/**
 * 阶梯计租
 */
function stepLease() {
	const { props: { form } } = this;
	/* 按阶梯租金计租 */
	let step_lease_flag = form.getFormItemsValue(basicInfoForm, 'step_lease_flag').value;
	if (step_lease_flag) {
		/* 禁用计租方式 */
		form.setFormItemsDisabled(basicInfoForm, { rent_way: true });
		/* 非必输计租方式 */
		form.setFormItemsRequired(basicInfoForm, { rent_way: false });
        /* 置空计租方式 */
        form.setFormItemsValue(basicInfoForm, { rent_way: { value: '', display: '' } });

		/* 禁用阶梯租金 */
		form.setFormItemsDisabled(basicInfoForm, { step_rent: true });

        /* 禁用租金标准 */
        form.setFormItemsDisabled(basicInfoForm, { rent_standard: true });
        /* 非必输租金标准 */
        form.setFormItemsRequired(basicInfoForm, { rent_standard: false });
	} else {
		/* 启用计租方式 */
		form.setFormItemsDisabled(basicInfoForm, { rent_way: false });
		/* 必输计租方式 */
		form.setFormItemsRequired(basicInfoForm, { rent_way: true });

		/* 启用阶梯租金 */
		form.setFormItemsDisabled(basicInfoForm, { step_rent: false });
	}

    
}

/**
 * 恢复使用权页签控制
 */
function reset() {
    const { props: { form } } = this;
    /* 启用计租方式 */
    form.setFormItemsDisabled(basicInfoForm, { rent_way: false });
    /* 必输计租方式 */
    form.setFormItemsRequired(basicInfoForm, { rent_way: true });
    /* 启用阶梯租金 */
	form.setFormItemsDisabled(basicInfoForm, { step_rent: false });
    /* 禁用拆分金额（含税） */
    form.setFormItemsDisabled(basicInfoForm, { split_renttax: true });
    /* 非必输拆分金额（含税） */
    form.setFormItemsRequired(basicInfoForm, { split_renttax: false });
    /* 禁用衔接处理方法 */
    form.setFormItemsDisabled(basicInfoForm, { cohesion_process: true });
    /* 禁用追溯日期 */
    form.setFormItemsDisabled(basicInfoForm, { retrospective_date: true });
    /* 禁用租金拆分 */
	form.setFormItemsDisabled(basicInfoForm, { rent_split_flag: true });
    /* 启用租金标准 */
    form.setFormItemsDisabled(basicInfoForm, { rent_standard: false });
    /* 必输租金标准 */
    form.setFormItemsRequired(basicInfoForm, { rent_standard: true });
}

export {
    enableUsufructAssetTab,
    disableUsufructAssetTab,
    emptyUsufructAssetTab,
    getUsufructAssetTab,
    setUsufructAssetTab,
    simpleTraceabilityDominate,
    stepLease,
    handleMeta,
    reset
};

